package com.probiz.estore.attribute.service.impl;

import java.util.List;

import com.probiz.estore.attribute.service.AttributeManager;
import com.probiz.estore.attribute.service.AttributeNtManager;
import com.probiz.estore.common.model.attribute.Attribute;
import com.probiz.estore.core.search.SearchCriteria;

public class AttributeNtManagerImpl implements AttributeNtManager {
	
	private AttributeManager attributeManager = null;

	public void setAttributeManager(AttributeManager attributeManager) {
		this.attributeManager = attributeManager;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeNtManager#clearInvalidAttributes()
	 */
	public void clearInvalidAttributes() {
		clearInvalidProductAttributes();
		// ADD OTHER

	}


	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.attribute.service.AttributeNtManager#clearInvalidProductAttributes()
	 */
	public void clearInvalidProductAttributes() {
		String hql = "from Attribute a where a.attributeType = 1 and a.attributeId not in (select i.attribute.attributeId from ProductAttGroupItem i)";
		boolean flag =true;
		int pageSize = 10;
		int pageNo = 1;
		do{
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql,null,pageNo,pageSize,null);
			List<Attribute> attributeList = attributeManager.searchByCriteria(sc);
			for(Attribute attribute :attributeList){
				try{
					attributeManager.deleteById(attribute.getAttributeId());
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(attributeList.size()!=pageSize){
				flag=false;
			}else{
				++pageNo;
			}
		}while(flag);	

	}

}
